package com.yangtao.springbootfreemarker.exception;

import com.yangtao.core.exception.ServiceException;
import com.yangtao.core.web.ResultMsg;
import org.apache.shiro.authz.AuthorizationException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * 自定义异常捕获
 */
@RestControllerAdvice
public class ExceptionCatch {

	/**
	 * 捕获自定义异常
	 * @param e
	 */
	@ExceptionHandler(ServiceException.class)
	@ResponseBody
	public ResultMsg catchServiceException(ServiceException e) {
		return ResultMsg.buildErrorMsg(e.getMessage());
	}

	/**
	 * 捕获Shiro权限异常
	 * @param e
	 */
	@ExceptionHandler(AuthorizationException.class)
	@ResponseBody
	public ResultMsg catchAuthorizationException(AuthorizationException e) {
		return ResultMsg.buildErrorMsg("没有权限，请联系管理员授权");
	}

	/**
	 * 捕获自定义异常
	 * @param e
	 */
	@ExceptionHandler(Exception.class)
	@ResponseBody
	public ResultMsg catchException(Exception e) {
		return ResultMsg.buildErrorMsg("系统错误");
	}
}
